﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    /// <summary>
    /// 筛查资源dal
    /// </summary>
    public class ScreenEquipmentDal
    {
        public DapperHelper dapperHelper { get; set; }

        /// <summary>
        /// 根据筛查ID,查询营销资源设备
        /// 原程序 ：ScreenInfoServiceImpl-> qryEquipmentIdsByScreenid(String screenid, DoctorUser doctorUser)
        /// </summary>
        /// <param name="screenId">筛查ID</param>
        /// <returns></returns>
        public IEnumerable<ScreenEquipmentView> QueryScreenEquipmentViewByScreenId(Guid screenId)
        {
            return dapperHelper.ExecueWithoutTransaction<IEnumerable<ScreenEquipmentView>>(a =>
            {
                string sql = $@"select * from new_new_screen_new_equipment where new_screenid='{screenId}'  ";
                var sqlTemplate = new SqlBuilder().AddTemplate(sql);

                return a.Query<ScreenEquipmentView>(sqlTemplate.RawSql);
            });
        }

        /// <summary>
        /// 根据筛查ID,查询营销资源设备
        /// </summary>
        /// <param name="screenId">筛查ID</param>
        /// <param name="equipmentId">营销资源设备ID</param>
        /// <returns></returns>
        public bool ExistsScreenEquipment(Guid screenId, Guid equipmentId)
        {
            return dapperHelper.ExecueWithoutTransaction<bool>(a =>
            {
                string sql = $@"select count(1) from new_new_screen_new_equipment where new_screenid='{screenId}' and new_equipmentid='{equipmentId}'  ";
                var sqlTemplate = new SqlBuilder().AddTemplate(sql);
                int result = a.QueryFirst<int>(sqlTemplate.RawSql);

                return result > 0 ? true : false;
            });
        }


    }
}
